package cj.web.marriageSC.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriageSC.dao.SCPropertyDAO;
import cj.web.marriageSC.dao.SCPropertyValueDAO;
import cj.web.marriageSC.domain.SCProperty;
import cj.web.marriageSC.domain.SCPropertyValue;

@Path("/propertyValue")
public class SCPropertyValueController extends BaseController{

	private static Logger logger = Logger.getLogger(SCPropertyValueController.class.getName());
	
	@Autowired
	private SCPropertyValueDAO propertyValueDao;
	
	@Autowired
	private SCPropertyDAO propertyDao;

	@Get("show")
	public String show(Model model,@Param("propertyId") int propertyId, SCPropertyValue propertyValue)
	{
		model.add("propertyId",propertyId);
		SCProperty property = propertyDao.queryById(propertyId);
		model.add("propertyName", property.getName());
		model.add("MemuEnum", MemuEnum.marriagescproperty);
		return "/views/marriageSC/proValList.vm";
	}
	
	@Post("list")
	public String list(Model model, SCPropertyValue propertyValue)
	{
		List<SCPropertyValue> objs = propertyValueDao.queryByPropertyId(propertyValue);
		int size = propertyValueDao.sizeByPropertyId(propertyValue);
		
		JSONObject jo = new JSONObject();
		jo.put("data", objs);
		jo.put("draw", propertyValue.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	@Get("create")
	public String create(Model model, @Param("propertyId")int propertyId)
	{
		model.add("propertyId",propertyId);
		SCProperty property = propertyDao.queryById(propertyId);
		model.add("categoryId", property.getCategoryId());
		model.add("MemuEnum", MemuEnum.marriagescproperty);
		return "/views/marriageSC/proValDetail.vm";
	}
	
	@Post("crtProVal")
	public String crtProVal(Model model,SCPropertyValue propertyValue, HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String acount = returnSessionObj(request).getAccount();
		propertyValue.setCreateUser(acount);
		propertyValue.setUpdateUser(acount);
		propertyValue.setDeleteFlag(1);
		
		try {
			propertyValueDao.create(propertyValue);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model, @Param("id") int id, HttpServletRequest request)
	{
		SCPropertyValue obj = propertyValueDao.queryById(id);
		model.add("obj", obj);
		model.add("MemuEnum", MemuEnum.marriagescproperty);
		return "/views/marriageSC/proValEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,SCPropertyValue propertyValue, HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		String acount = returnSessionObj(request).getAccount();
		propertyValue.setUpdateUser(acount);
		
		try {
			propertyValueDao.update(propertyValue);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		return "@" + jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id, HttpServletRequest request)
	{
		SCPropertyValue propertyValue = propertyValueDao.queryById(id);
		if(propertyValue != null){
			String acount = returnSessionObj(request).getAccount();
			
			propertyValue.setUpdateUser(acount);
			propertyValueDao.deleteById(propertyValue);
		}
		
		return "r:show?propertyId=" + propertyValue.getPropertyId();
	}
}
